﻿Ext.define("PSI.InConsignation.ConsignationEditForm", {
    createUI: function (entity) {
        var me = this;

        var wnd = Ext.create("Ext.window.Window", {
            title: entity == null ? "新增进口委托" : "编辑进口委托",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 500,
            height: 260,
            layout: "fit",
            items: [
                {
                    id: "editForm",
                    xtype: "form",
                    layout: "form",
                    height: "100%",
                    bodyPadding: 5,
                    defaultType: 'textfield',
                    fieldDefaults: {
                        labelWidth: 60,
                        labelAlign: "right",
                        labelSeparator: "",
                        msgTarget: 'side'
                    },
                    items: [
                        {
                            xtype: "hidden",
                            name: "id",
                            value: entity == null ? null : entity.id
                        },
                        {
                            xtype: "hidden",
                            name: "billNumber",
                            value: entity == null ? null : entity.billNumber
                        },
                        {
                            id: "editBillNumber",
                            fieldLabel: "委托单号",
                            xtype: "displayfield",
                            value: entity == null ? "[保存后系统自动生成]" : entity.billNumber
                        },
                        {
                            xtype: "psi_bc_vslfield",
                            id: "editVsl",
                            fieldLabel: "船名",
                            allowBlank: false,
                            blankText: "没有输入船名",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            name: "vsl"
                        }, {
                            id: "editVoyNumber",
                            fieldLabel: "航次",
                            name: "voyNumber"
                        }, {
                            xtype: "psi_bc_clientsfield",
                            id: "editClients",
                            fieldLabel: "委托方",
                            allowBlank: false,
                            blankText: "没有输入委托方",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            name: "clients"
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "送货时限",
                            msgTarget: 'side',
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            layout: "hbox",
                            items: [
                                {
                                    id: "editDeliverDate",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    name: "deliverDate",
                                    allowBlank: false,
                                    blankText: "没有输入时限日期",
                                    flex: 1
                                }, {
                                    id: "editDeliverTime",
                                    xtype: "timefield",
                                    format: "H:i",
                                    allowBlank: false,
                                    blankText: "没有输入时限时间",
                                    name: "deliverTime",
                                    flex: 1
                                }
                            ]
                        }, {
                            id: "editBizMan",
                            xtype: "psi_bc_bizmanfield",
                            fieldLabel: "业务员",
                            allowBlank: false,
                            blankText: "没有输入业务员",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            name: "bizMan"
                        }, {
                            id: "editBizDT",
                            xtype: "datefield",
                            fieldLabel: "接单日期",
                            allowBlank: false,
                            blankText: "没有输入接单日期",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            format: "Y-m-d",
                            name: "bizDT"
                        }
                    ],
                    buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: me.onOK,
                            scope: me
                        },
                        {
                            text: "取消", handler: function () {
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.wnd.close();
                                });
                            }, scope: me
                        }
                    ]
                }
            ]

        });

        me.wnd = wnd;

        Ext.getCmp("editVsl").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editVoyNumber").focus();
            }
        }, me);
        Ext.getCmp("editVoyNumber").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editClients").focus();
            }
        }, me);
        Ext.getCmp("editClients").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDeliverDate").focus();
            }
        }, me);
        Ext.getCmp("editDeliverDate").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDeliverTime").focus();
            }
        }, me);
        Ext.getCmp("editDeliverTime").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editBizMan").focus();
            }
        }, me);
        Ext.getCmp("editBizMan").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editBizDT").focus();
            }
        }, me);
    },

    show: function (parentForm, entity) {
        var me = this;

        me.parentForm = parentForm;

        me.createUI(entity);

        me.wnd.on("show", function () { me.onEditFormShow(entity); }, me);

        me.wnd.show();
    },

    onEditFormShow: function (entity) {
        var me = this;

        Ext.getCmp("editVsl").focus();
        if (!entity) {
            // 设置新增的时候的字段的默认值
            Ext.getCmp("editBizMan").setValue(me.parentForm.userName);

            Ext.getCmp("editBizDT").setValue(new Date());
            return;
        }

        var form = me.wnd;
        var el = me.wnd.getEl() || Ext.getBody();

        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/InConsignation/GetInConsignation",
            method: "POST",
            params: { id: entity.id },
            callback: function (options, success, response) {
                el.unmask();
                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    me.setEntityData(data);
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }
            }
        });
    },

    setEntityData: function (data) {
        Ext.getCmp("editVsl").setValue(data.vsl);
        Ext.getCmp("editVoyNumber").setValue(data.voyNumber);
        Ext.getCmp("editClients").setValue(data.clients);
        Ext.getCmp("editDeliverDate").setValue(data.deliverDate);
        Ext.getCmp("editDeliverTime").setValue(data.deliverTime);
        Ext.getCmp("editBizMan").setValue(data.bizMan);
        Ext.getCmp("editBizDT").setValue(data.bizDT);
    },

    // private
    onOK: function () {
        var me = this;

        var f = Ext.getCmp("editForm");
        var el = f.getEl() || Ext.getBody();
        el.mask("数据保存中...");
        f.submit({
            url: "/InConsignation/EditInConsignation",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                me.wnd.close();
                me.parentForm.refreshInConsignationGrid(action.result.id);
            },
            failure: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo(action.result.msg, function () {
                    Ext.getCmp("editVsl").focus();
                });
            }
        });
    }
});